package com.haoxin.ai_course.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoxin.ai_course.authentication.TokenGenerator;
import com.haoxin.ai_course.common.utils.R;
import com.haoxin.ai_course.modules.sys.entity.SysUserToken;
import com.haoxin.ai_course.modules.sys.mapper.SysUserTokenMapper;
import com.haoxin.ai_course.modules.sys.service.SysUserTokenService;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
@AllArgsConstructor
public class SysUserTokenServiceImpl extends ServiceImpl<SysUserTokenMapper,SysUserToken> implements SysUserTokenService {

	private final SysUserTokenMapper sysUserTokenMapper;
	//12小时后过期
	private final static int EXPIRE = 3600 * 12* 1000;


	@Override
	public R createToken(long userId) {
		//生成一个token
		String token = TokenGenerator.generateValue();
		//当前时间
		Date now = new Date();
		//过期时间
		Date expireTime = new Date(now.getTime() + EXPIRE );
		//判断是否生成过token
		SysUserToken tokenEntity = sysUserTokenMapper.selectById(userId);
		if(tokenEntity == null){
			tokenEntity = new SysUserToken();
			tokenEntity.setUserId(userId);
			tokenEntity.setToken(token);
			tokenEntity.setUpdateTime(now);
			tokenEntity.setExpireTime(expireTime);
			//保存token
			baseMapper.insert(tokenEntity);
		}else if(tokenEntity.getExpireTime().getTime() < System.currentTimeMillis()){
			tokenEntity.setToken(token);
			tokenEntity.setUpdateTime(now);
			tokenEntity.setExpireTime(expireTime);
			//更新token
			sysUserTokenMapper.updateById(tokenEntity);
		}
		R r = R.ok().put("token", tokenEntity.getToken()).put("expire", EXPIRE);
		return r;
	}
	@Override
	public SysUserToken createUserToken(long userId) {
		//生成一个token
		String token = TokenGenerator.generateValue();
		//当前时间
		Date now = new Date();
		//过期时间
		Date expireTime = new Date(now.getTime() + EXPIRE );
		
		//判断是否生成过token
		SysUserToken tokenEntity = sysUserTokenMapper.selectById(userId);
		if(tokenEntity == null){
			tokenEntity = new SysUserToken();
			tokenEntity.setUserId(userId);
			tokenEntity.setToken(token);
			tokenEntity.setUpdateTime(now);
			tokenEntity.setExpireTime(expireTime);
			
			//保存token
			baseMapper.insert(tokenEntity);
		}else if(tokenEntity.getExpireTime().getTime() < System.currentTimeMillis()){
			tokenEntity.setToken(token);
			tokenEntity.setUpdateTime(now);
			tokenEntity.setExpireTime(expireTime);
			//更新token
			sysUserTokenMapper.updateById(tokenEntity);
		}
		R r = R.ok().put("token", token).put("expire", EXPIRE);
		
		return tokenEntity;
	}

	@Override
	public void logout(long userId) {
//		sysUserTokenMapper.deleteById(userId);
	}

	@Override
	public Integer removeAllToken(QueryWrapper<SysUserToken> queryWrapper) {
		// TODO Auto-generated method stub
		return sysUserTokenMapper.removeAllToken(queryWrapper);
	}

	@Override
	public SysUserToken getTokenByUserId(QueryWrapper<SysUserToken> qw) {
		// TODO Auto-generated method stub
		return sysUserTokenMapper.getTokenByUserId(qw);
	}
}
